/*
 * SmartEvent.java
 *
 * Created on 21 February 2005, 00:05
 *
 * Copyright 2008 the Desert Labs Project (http://desertlabs.us)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package us.desertlabs.event;

/**
 * This is the base class for any type of event that can be handled by the
 * <CODE>SmartEventBus</CODE>.
 *
 * @param <S> the datatype of <CODE>Object</CODE>s that can be the source of
 *        this kind of event
 * @author Brandon Franklin
 */
public abstract class SmartEvent<S> {

    /** The <CODE>Object</CODE> that was the source of the event */
    private final S source;

    /** The nanosecond when the event was created */
    private final long timestamp;

    /**
     * Creates a new instance of <CODE>SmartEvent</CODE>, originating from
     * the supplied source <CODE>Object</CODE>.
     *
     * @param source the <CODE>Object</CODE> that was the source of the event
     */
    protected SmartEvent( final S source ) {
        this.source = source;

        timestamp = System.nanoTime();
    }

    /**
     * Returns the <CODE>Object</CODE> that was the source of the event.
     *
     * @return the <CODE>Object</CODE> that was the source of the event
     */
    public S getSource() {
        return source;
    }

    /**
     * Returns the nanosecond when the event was created.
     *
     * @return the nanosecond when the event was created
     */
    public long getTimestamp() {
        return timestamp;
    }

    /**
     * Returns a <CODE>String</CODE> representation of this event.
     *
     * @return a <CODE>String</CODE> representation of this event
     */
    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();

        sb.append( super.toString() );
        sb.append( " from " );
        sb.append( source.toString() );

        return sb.toString();
    }
}
